package nc.impl.uap.busibean.cil;

import com.cn.bup.CesCilCtl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import nc.itf.uap.cil.ICilService;







public class CilImpl
  implements ICilService
{
  public CilImpl() {}
  
  private List<String> cpuLicList = null;
  
  private List<String> getCpuLicList() { if (this.cpuLicList == null) {
      this.cpuLicList = new ArrayList();
      this.cpuLicList.add("1221");
      this.cpuLicList.add("E001");
    }
    return this.cpuLicList; }
  
  private Map<String, Integer> hm_SpecialControl = null;
  
  private Map<String, Integer> getHm_SpecialControl() { if (this.hm_SpecialControl == null) {
      this.hm_SpecialControl = new HashMap();
      this.hm_SpecialControl.put("2501", Integer.valueOf(Integer.MAX_VALUE));
      this.hm_SpecialControl.put("4001", Integer.valueOf(Integer.MAX_VALUE));
      this.hm_SpecialControl.put("6001", Integer.valueOf(Integer.MAX_VALUE));
    }
    
    return this.hm_SpecialControl;
  }
  





  public int getProductLicense(String product)
  {
    if (getHm_SpecialControl().containsKey(product)) {
      return ((Integer)getHm_SpecialControl().get(product)).intValue();
    }
    
    int licCount = -1;
    
    Map licMap = Collections.synchronizedMap(CesCilCtl.getInstance()
      .getProductLicenses());
    if (licMap.containsKey(product)) {
      licCount = ((Integer)licMap.get(product)).intValue();
    } else if (licMap.containsKey("*")) {
      if (product.equals("corp")) {
        licCount = 500;
      } else
        licCount = ((Integer)licMap.get("*")).intValue();
    } else {
      licCount = -1;
    }
    
    if (getCpuLicList().contains(product)) {
      licCount = licCount > 0 ? Integer.MAX_VALUE : -1;
    }
    return licCount;
  }
  




  public String getProductCode(String moduleCode)
  {
    if (isJZFDC(moduleCode)) {
      return getJZFDCProductCode(moduleCode);
    }
    
    Iterator<String> iter = getHm_SpecialControl().keySet().iterator();
    while (iter.hasNext()) {
      String product = (String)iter.next();
      if (moduleCode.startsWith(product)) {
        return product;
      }
    }
    
    return CesCilCtl.getInstance().getProductCode(moduleCode);
  }
  
  private boolean isJZFDC(String moduleCode) { if ((moduleCode == null) || (moduleCode.length() < 2) || 
      (moduleCode.substring(2, 3).equalsIgnoreCase("H"))) {
      return false;
    }
    String code = moduleCode.substring(0, 2);
    String code4 = moduleCode.length() >= 4 ? moduleCode.substring(0, 4) : "";
    

    if (("H0".equalsIgnoreCase(code)) && ((code4.contains("H0K0")) || (code4.contains("H0K3"))))
    {
      return false;
    }
    


    return (code.equalsIgnoreCase("H0")) || (code.equalsIgnoreCase("H1")) || (code.equalsIgnoreCase("H3")) || (code.equalsIgnoreCase("H4"));
  }
  
  private String getJZFDCProductCode(String moduleCode) {
    if (moduleCode.length() >= 6) {
      if (("H30303".equalsIgnoreCase(moduleCode)) || ("H30200".equalsIgnoreCase(moduleCode))) {
        return "10";
      }
      HashMap<String, Integer> map = CesCilCtl.getInstance().getProductLicenses();
      String code = moduleCode.substring(0, 6);
      Integer value = (Integer)map.get(code);
      if (value != null) {
        if (value.intValue() > 1)
          return code;
        if (value.intValue() == 1) {
          return CesCilCtl.getInstance().getProductCode(moduleCode);
        }
      }
      return code;
    }
    return CesCilCtl.getInstance().getProductCode(moduleCode);
  }
  




  public boolean isUsedGLBook()
  {
    return CesCilCtl.getInstance().isUsedGLBook();
  }
  




  public boolean isNCDEMO() {
//    Map licMap = Collections.synchronizedMap(CesCilCtl.getInstance()
//      .getProductLicenses());
//    if (licMap.containsKey("*"))
//      return true;
//    return false;
	  return false;
  }
  
  public HashMap<String, Integer> getAllProducts() {
    HashMap<String, Integer> hm = CesCilCtl.getInstance().getAllProducts();
    hm.putAll(getHm_SpecialControl());
    return hm;
  }
}